package com.tsmti.util;

import com.alibaba.fastjson.JSON;
import com.tsmti.basicdata.entity.DeptInfomation;
import com.tsmti.basicdata.service.DeptInfomationService;
import com.tsmti.core.common.constant.RedisConstants;
import com.tsmti.core.util.RedisUtils;
import com.tsmti.core.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package：com.tsmti.lab.util
 * @Class：DoctorUitls
 * @Description： TODO
 * @Author：dcd
 * @Date：Created in 2018/7/19 下午4:02
 * @Company:
 * @Version：
 * @Modified By:
 */
public class DeptUtils {
    private static Logger logger = LoggerFactory.getLogger(DeptUtils.class);
    private static DeptInfomationService deptInfomationService = SpringContextUtils.getBean(DeptInfomationService.class);



    /**
     * 根据code获取姓名
     * @param code code
     * @return 姓名
     */
    public static String getNameByCode(String code) {
        Map<String,DeptInfomation> allMap = loadCache();
        if(allMap.containsKey(code)){
            return allMap.get(code).getName();
        }
        return "";
    }

    /**
     * 根据code获取对象
     * @param code code
     * @return 对象
     */
    public static DeptInfomation getValue(String code) {
        Map<String,DeptInfomation> allMap = loadCache();
        if(allMap.containsKey(code)){
            return allMap.get(code);
        }
        return null;
    }

    /**
     * 科室MAP
     * @return 科室MAP
     */
    public static Map<String, String> getDepatMap() {
        Map<String, DeptInfomation> map = loadCache();
        Map<String, String> mapTemp = new HashMap<String,String>(16);
        for (String key : map.keySet()) {
            mapTemp.put(key,map.get(key).getName());
        }
        return mapTemp;
    }

    /**
     * 加载缓存
     */
    private static Map<String, DeptInfomation> loadCache() {
        Map<String, DeptInfomation> deptInfomationMap = new HashMap<String, DeptInfomation>(16);
        try {
            deptInfomationMap = (Map<String, DeptInfomation>) RedisUtils.getObject(RedisConstants.DEPT_CACHE);
            if (deptInfomationMap == null) {
                deptInfomationMap = new HashMap<String, DeptInfomation>(16);
            }
        } catch (Exception e) {
            logger.warn("读取医生护士缓存失败!");
            e.printStackTrace();
        }
        if (deptInfomationMap == null || deptInfomationMap.size() <= 0) {
            List<DeptInfomation> deptInfomationList = deptInfomationService.list();
            try {
                RedisUtils.setRedisToMap(RedisConstants.DEPT_CACHE,"code", deptInfomationList);
            } catch (Exception e) {
                logger.warn("加载血液类型缓存错误!", e);
            }
        }
        return deptInfomationMap;
    }

    /**
     * 部门列表
     * @param orgId 所属组织ID
     * @return 部门列表
     */
    public static List<DeptInfomation> getDeptList(String orgId){
        List<DeptInfomation>  deptInfomationList = new ArrayList<>();
        try {
            deptInfomationList = JSON.parseArray((String) RedisUtils.getObject(RedisConstants.DEPT_CACHE),DeptInfomation.class);
            if(deptInfomationList == null || deptInfomationList.size() == 0){
                deptInfomationList =  deptInfomationService.list("orgId",orgId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deptInfomationList;
    }
}
